<template>
  <div class="login">
    <!-- 粒子背景 -->
    <!--
      color: String类型。默认'#dedede'。粒子颜色。
      particleOpacity: Number类型。默认0.7。粒子透明度。
      particlesNumber: Number类型。默认80。粒子数量。
      shapeType: String类型。默认'circle'。可用的粒子外观类型有："circle","edge","triangle", "polygon","star"。
      particleSize: Number类型。默认80。单个粒子大小。
      linesColor: String类型。默认'#dedede'。线条颜色。
      linesWidth: Number类型。默认1。线条宽度。
      lineLinked: 布尔类型。默认true。连接线是否可用。
      lineOpacity: Number类型。默认0.4。线条透明度。
      linesDistance: Number类型。默认150。线条距离。
      moveSpeed: Number类型。默认3。粒子运动速度。
      hoverEffect: 布尔类型。默认true。是否有hover特效。
      hoverMode: String类型。默认true。可用的hover模式有: "grab", "repulse", "bubble"。
      clickEffect: 布尔类型。默认true。是否有click特效。
      clickMode: String类型。默认true。可用的click模式有: "push", "remove", "repulse", "bubble"。
    -->
    <vue-particles
      color="#1256e6"
      :particleOpacity="1"
      :particlesNumber="60"
      shapeType="star"
      :particleSize="4"
      linesColor="#1256e6"
      :linesWidth="1"
      :lineLinked="true"
      :lineOpacity="0.7"
      :linesDistance="150"
      :moveSpeed="2"
      :hoverEffect="true"
      hoverMode="grab"
      :clickEffect="true"
      clickMode="push"
      class="background"
    >

    </vue-particles>
    <!-- 顶部登录成功alert -->
    <v-alert class="login-tip"
      :value="loginSuccess"
      dense
      text
      type="success"
      transition="scale-transition"
    >
      登录成功
    </v-alert>

    <v-card-actions style="margin: 0 auto;" v-if="resetLoading">
      <!-- 粒子特效按钮, 点击登录时开始隐藏按钮 -->
      <ParticleEffect
        ref="particleEffectRef"
        :hidden="loading"
        direction="bottom"
        particle-type="circle"
        particle-style="fill"
        particle-color="#FFB6C1"
        :duration="500"
        easing="easeInOutCubic"
        :canvas-padding="10"
        :size="4"
        :speed="2"
        :particles-amount-coefficient="3"
        :oscillation-coefficient="20"
        @begin="onBegin"
        @complete="onComplete"
        style="width: 440px;display: grid!important;"
      >
        <v-row justify="center" style="width: 440px;margin: 0 auto">
          <v-col
            cols="12"
          >
            <v-card :rules="loginRules" class="login-form">
              <div class="title">
                <div style="width: 25%;" title="RuoYi Logo">
                  <v-img
                    style="position: fixed;"
                    alt="Ruoyi Logo"
                    :src="require('../assets/logo/logo.png')"
                    transition="scale-transition"
                    width="40"
                    height="50"
                  />
                </div>
                <div style="width: 25%;margin-left: 50px" title="Vuetify Logo">
                  <v-img
                    style="position: fixed;object-fit: cover;max-height: 33px;margin-top: 7px"
                    alt="Vuetify Logo"
                    :src="require('../assets/logo.svg')"
                    transition="scale-transition"
                    width="40"
                  />
                </div>
                <div style="margin-left: 40px">
                  <div style="margin: 0 auto;padding-top: 6px;">
                    若依后台管理系统
                  </div>
                </div>
              </div>
              <!-- 登录表单 -->
              <v-form ref="loginForm" v-model="valid" >
                <v-card-text style="padding-bottom: 0;">
                  <v-text-field v-ripple style="border-radius: 12px"
                    v-model="loginForm.username"
                    :rules="loginRules.username"
                    label="账号"
                    placeholder="请输入账号"
                    required
                    clearable
                    prepend-inner-icon="mdi-account"
                  ></v-text-field>
                  <v-text-field v-ripple style="border-radius: 12px"
                    v-model="loginForm.password"
                    :rules="loginRules.password"
                    label="密码"
                    placeholder="请输入密码"
                    required
                    clearable
                    type="password"
                    prepend-inner-icon="mdi-lock"
                  ></v-text-field>
                  <v-text-field v-ripple
                    v-model="loginForm.code"
                    :rules="loginRules.code"
                    label="验证码"
                    placeholder="请输入验证码"
                    required
                    clearable
                    prepend-inner-icon="mdi-cursor-default-click"
                    style="width: 200px;z-index: 1000;border-radius: 12px"
                    @keyup.enter.native="handleLogin"
                  >
                  </v-text-field>
                  <!-- 验证码图片 -->
                  <div class="login-code" title="点击更换验证码" v-ripple>
                    <v-img :src="codeUrl" @click="getCode" class="login-code-img" alt="alt"/>
                  </div>
                </v-card-text>
                <div style="height: 50px">
                  <v-btn v-ripple
                         color="primary"
                         :loading="loginLoading"
                         block
                         @click="handleLogin" style="font-size: 14px;width: 240px;"
                  >
                    登录
                  </v-btn>
                </div>
              </v-form>
            </v-card>
          </v-col>
        </v-row>
      </ParticleEffect>
    </v-card-actions>
    <!--  底部  -->
    <div class="el-login-footer">
      <span>Copyright © 2018-2021 ruoyi.vip All Rights Reserved.</span>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt';
import ParticleEffect from '@/components/Particle/particle-effect';

export default {
  name: "Login",
  components: {
    ParticleEffect
  },
  data() {
    return {
      codeUrl: "",
      cookiePassword: "",
      loginForm: {
        username: "",
        // username: "admin",
        password: "",
        // password: "admin123",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        username: [
          val => (val || '').length > 0 || '账号不能为空'
        ],
        password: [
          val => (val || '').length > 0 || '密码不能为空'
        ],
        code: [
          val => (val || '').length > 0 || '请输入验证码'
        ],
      },
      valid: true,
      loading: false,
      redirect: undefined,
      formHasErrors: true,
      loginSuccess: false,
      resetLoading: true,
      timer: 0,
      loginLoading: false,
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    // 获取验证码
    getCode() {
      //this.loginLoading = false;
      getCodeImg().then(res => {
        this.codeUrl = "data:image/gif;base64," + res.img;
        this.loginForm.uuid = res.uuid;
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    // 登录
    async handleLogin() {
      this.$refs.loginForm.validate();
      if (this.valid) {
        this.loginLoading = true;
        if (this.loginForm.rememberMe) {
          Cookies.set("username", this.loginForm.username, { expires: 30 });
          Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
          Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
        } else {
          Cookies.remove("username");
          Cookies.remove("password");
          Cookies.remove('rememberMe');
        }
        this.$store.commit("snackbar/CLOSE_SNACKBAR");
        await this.$store.dispatch("Login", this.loginForm).then(() => {
          this.loginSuccess = true;
          this.loading = true; // 动画开始
          this.loginLoading = false;
        }).catch(() => {
          this.getCode();
          this.loginLoading = false;
        })
      }
    },
    onBegin() {
      this.loading = false;
    },
    onComplete() {
      this.resetLoading = false;
      setTimeout(() => {
        this.loginSuccess = false;
        this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
      }, 800);
      this.timer++ ;
    },
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
  .background {
    width: 100%;
    height: 100%;
    position: fixed;
  }
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-image: url("../assets/images/login-background.jpg");
    background-size: cover;
  }

  .title {
    margin: 0 auto 0 20px;
    color: #707070;
    height: 46px;
    vertical-align: middle;
  }

  .login-form {
    border-radius: 6px;
    background: transparent;
    width: 400px;
    text-align: center;
    margin: 0 auto;
    padding: 22px 22px 5px 22px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon {
      height: 39px;
      width: 14px;
      margin-left: 2px;
    }
  }

  .login-tip {
    text-align: center;
    color: #bfbfbf;
    position: fixed !important;
    top: 10px;
    font-weight: 450;
  }

  .login-code {
    margin-top: -60px;
    margin-left: -15px;
    width: 33%;
    height: 38px;
    float: right;

    img {
      cursor: pointer;
      vertical-align: middle;
    }
  }

  .el-login-footer {
    height: 40px;
    line-height: 40px;
    position: fixed;
    bottom: 0;
    width: 100%;
    text-align: center;
    color: #fff;
    font-family: Arial;
    font-size: 12px;
    letter-spacing: 1px;
  }

  .login-code-img {
    height: 38px;
    //border-radius: 12px;
  }
</style>
